package com.ideaaedi.component.compile;

import javax.tools.SimpleJavaFileObject;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

/**
 * (non-javadoc)
 *
 * @author <a href="https://github.com/alibaba/arthas">Arthas</a>
 * @author JustryDeng
 * @since 2021/10/9 21:21:04
 */
public class ClassJavaFileObject extends SimpleJavaFileObject {
    private final String classLongName;
    private final URI uri;
    
    public ClassJavaFileObject(String classLongName, URI uri) {
        super(uri.getPath() == null ? URI.create("Klass://" + uri.getSchemeSpecificPart()) : uri, Kind.CLASS);
        this.uri = uri;
        this.classLongName = classLongName;
    }
    
    @Override
    public URI toUri() {
        return uri;
    }
    
    @Override
    public InputStream openInputStream() throws IOException {
        return uri.toURL().openStream();
    }
    
    @Override
    public String getName() {
        return classLongName;
    }
    
    @Override
    public Kind getKind() {
        return Kind.CLASS;
    }
    
    @Override
    public boolean isNameCompatible(String simpleName, Kind kind) {
        String baseName = simpleName + kind.extension;
        return kind.equals(getKind())
                && (baseName.equals(getName())
                || getName().endsWith("/" + baseName));
    }
    
    @Override
    public String toString() {
        return this.getClass().getName() + "[" + this.toUri() + "]";
    }
}

